package com.itheima.d4_synchronized_code;

class Ticket01 implements Runnable {
    private int ticket = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (ticket > 0) {
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + " 在卖票，剩余票数为" + ticket);
                } else {
                    break;
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
